package pl.perseus.service.user;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("assembler")
public class Assembler {

  @Transactional(readOnly = true)
  User buildUserFromUserEntity(pl.perseus.model.User userEntity) {

    String username = userEntity.getLogin();
    String password = userEntity.getPassword();
    boolean enabled = userEntity.getActive();
    boolean accountNonExpired = userEntity.getActive();
    boolean credentialsNonExpired = userEntity.getActive();
    boolean accountNonLocked = userEntity.getActive();

    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    /*for (SecurityRoleEntity role : userEntity.getRoles()) {
      authorities.add(new GrantedAuthorityImpl(role.getRoleName()));
    }*/

    User user = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    return user;
  }
}

